package com.zy.je.common.mybatis.ext.injector.util;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.commons.lang3.ClassUtils;

import java.util.Date;

/**
 * @author zy
 */
public class LogicDeleteUtils {

    public static final String DELETE_TIME_FIELD_NAME = "delete_time";

    public LogicDeleteUtils() {
    }

    public static String sqlLogicSet(TableInfo table) {
        String set = "SET " + table.getLogicDeleteSql(false, false);
        if (hasDeleteTimeField(table)) {
            set = String.format("%s ,%s=NOW() ", set, DELETE_TIME_FIELD_NAME);
        }
        //如果想在这里设置操作员姓名，是不可行的。因为逻辑删除sql只在启动时注入一次
        return set;
    }
    private static boolean hasDeleteTimeField(TableInfo table) {
        return table.getFieldList().stream().anyMatch(
                v -> v.getColumn().equals(DELETE_TIME_FIELD_NAME)
                        && (ClassUtils.isAssignable(v.getPropertyType(), Long.class) || ClassUtils.isAssignable(v.getPropertyType(), Date.class))
        );
    }

}
